<?php
/**
 * Created by PhpStorm.
 * AdminUser: Administrator
 * Date: 2020/11/18
 * Time: 16:17
 */
namespace app\admin\controller;

use app\BaseController;
use app\common\model\User;
use think\facade\Db;
use think\facade\View;
//use app\common\model\AdminUser;

class Login extends AdminBase
{
    public function initialize()
    {
        if ($this->isLogin()) {
            return $this->redirect(url("/index/index"));
        }
    }

    public function index()
    {
        return View::fetch('login');
    }

    public function md5()
    {
        return md5('test');
    }

    public function login_auth()
    {
        $username = $this->request->post('username');
        $password = $this->request->post('password');

        $notice = '';
        if (empty($username) || empty($password)) {
            $notice = '用户名或密码不能为空';
        } else {
            $user_info = _uri("user", array("user_name"=>$username));
            //dump($user_info);die;

            if (empty($user_info)) {
                $notice = '登录失败，用户名错误';
            } else if ($user_info['password'] != User::set_pwd(trim($password), $user_info['hash'])) {

                $notice = '登录失败，密码错误';
            } else if ($user_info['status'] == 0) {
                $notice = '登录失败，你的帐户不存在';
            }
        }

        if ($notice) {
            View::assign('notice', $notice);
            View::assign('username', $username);
            View::assign('password', $password);
            return View::fetch('/login/login');
        }

//        if (!$user_info['token']) {
//            User::update_access_token($user_info['id']);
//        }

        User::remember_me_set($user_info);
        if ($user_info['type'] == 10) {
            // city管理员登录后，直接跳到city管理页
           return redirect(url('city/controller'));
        } else {
            return redirect(url('/open_card/admin/index'));
        }
    }

    public function test()
    {
        echo 1123;exit;
    }
}